#!/usr/bin/env python

from pyftpdlib import ftpserver

import os
class EvilFS:
    def __init__(self):
        self.root = None
        self.cwd = '/'
        self.rnfr = None

    # --- Pathname / conversion utilities

    def ftpnorm(self, ftppath):
        return "/"

    def ftp2fs(self, ftppath):
        return "/"

    def fs2ftp(self, fspath):
        return "/"

    def validpath(self, path):
        return True

    

    def open(self, filename, mode):
        return 0

    def mkstemp(self, suffix='', prefix='', dir=None, mode='wb'):
        return ""


    def chdir(self, path):
        print "chdir"

    def mkdir(self, path):
        print "mkdir"

    def listdir(self, path):
        return "listdir a b c"

    def rmdir(self, path):
        print "rmdir"

    def remove(self, path):
        print "remove"

    def rename(self, src, dst):
        print "rename"

    def stat(self, path):
        return "a"

    def lstat(self, path):
        return "la"

    if not hasattr(os, 'lstat'):
        lstat = stat

    # --- Wrapper methods around os.path.*

    def isfile(self, path):
        return true

    def islink(self, path):
        return false

    def isdir(self, path):
        return true

    def getsize(self, path):
        return 0

    def getmtime(self, path):
        return 0

    def realpath(self, path):
        return "/"

    def lexists(self, path):
        return true

    def get_list_dir(self, path):
        return []

    def format_list(self, basedir, listing, ignore_err=True):
        return ""

    def format_mlsx(self, basedir, listing, perms, facts, ignore_err=True):
        return ""





class EvilAuthorizer(ftpserver.DummyAuthorizer):
	def __init__(self):
		return
		
	def add_user(self, username, password, homedir, perm='elr', msg_login="Login successful.", msg_quit="Goodbye."):
		return
	
	def add_anonymous(self, homedir, **kwargs):
		return
	
	def remove_user(self, username):
		return
	
	def override_perm(self, username, directory, perm, recursive=False):
		return

	def validate_authentication(self, username, password):
		return True

	def impersonate_user(self, username, password):
		return

	def terminate_impersonation(self):
		return

	def has_user(self, username):
		return True

	def has_perm(self, username, perm, path=None):
		return True

	def get_perms(self, username):
		return ""

	def get_home_dir(self, username):
		return "/"

	def get_msg_login(self, username):
		return "login"

	def get_msg_quit(self, username):
		return "quit"



		

class EvilFTP(ftpserver.FTPHandler):
#	def ftp_PORT(self, line):
#		self.respond("502 not implemented")
#		return

	abstracted_fs = EvilFS
	
	def ftp_EPRT(self, line):
		self.respond("502 not implemented")
		return
	
#	def ftp_PASV(self, line):
#		self.respond("502 not implemented")
#		return
	
	def ftp_EPSV(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_QUIT(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_LIST(self, path):
		listing=[
		"-rw-rw-rw-   1 owner   group    7045120 Sep 02  3:47 music.mp3",
        "drwxrwxrwx   1 owner   group          0 Aug 31 18:50 e-books",
        "-rw-rw-rw-   1 owner   group        380 Sep 02  3:40 module.py"]
		data = '\r\n'.join(listing) + '\r\n'
		self.push_dtp_data(data)
	
	def ftp_NLST(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_MLST(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_MLSD(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_RETR(self, file):
		self.respond("502 not implemented")
		return
	
	def ftp_STOR(self, file, mode='w'):
		self.respond("502 not implemented")
		return
	
	def ftp_STOU(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_APPE(self, file):
		self.respond("502 not implemented")
		return
	
	def ftp_REST(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_ABOR(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_USER(self, line):
		self.respond("331 send pass")
		return
	
	def ftp_PASS(self, line):
		self.respond("230 ok")
		self.authenticated = True
		return
	
	def ftp_REIN(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_PWD(self, line):
		self.respond('257 "%s" is the current directory.' % "/test")
		return
	
	def ftp_CWD(self, path):
		self.respond('250 "%s" is the current directory.' % "/")
	
	def ftp_CDUP(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_SIZE(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_MDTM(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_MKD(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_RMD(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_DELE(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_RNFR(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_RNTO(self, path):
		self.respond("502 not implemented")
		return
	
#	def ftp_TYPE(self, line):
#		self.respond("502 not implemented")
#		return
	
	def ftp_STRU(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_MODE(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_STAT(self, path):
		self.respond("502 not implemented")
		return
	
	def ftp_FEAT(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_OPTS(self, line):
		self.respond("502 not implemented")
		return
	
#def ftp_NOOP(self, line):
#	self.respond("502 not implemented")
#	return
	
	def ftp_SYST(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_ALLO(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_HELP(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_SITE_HELP(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_XCUP(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_XCWD(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_XMKD(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_XPWD(self, line):
		self.respond("502 not implemented")
		return
	
	def ftp_XRMD(self, line):
		self.respond("502 not implemented")
		return	
	

	
	
	
	
	

if __name__ == '__main__':
		
		ftp_handler=EvilFTP
		EvilFTP.authorizer = EvilAuthorizer()
		address = ('', 21)

		ftpd = ftpserver.FTPServer(address, ftp_handler)
		ftpd.serve_forever()

		